package bjtu.tjj.simpledy.utils;

import bjtu.tjj.simpledy.common.ResultCode;
import bjtu.tjj.simpledy.dto.TokenAndUidDTO;
import bjtu.tjj.simpledy.exception.CommonException;

public class ThreadLocalUtils {
    private final static ThreadLocal<TokenAndUidDTO> TOKEN_AND_UID = new ThreadLocal<>();

    public static void set(String token, long userId) {
        TOKEN_AND_UID.set(new TokenAndUidDTO(token, userId));
    }

    public static TokenAndUidDTO get() {
        TokenAndUidDTO dto = TOKEN_AND_UID.get();
        if (dto == null) {
            throw new CommonException(ResultCode.NOT_LOGIN);
        }
        return dto;
    }

    public static String getToken() {
        return get().token;
    }

    public static long getUserId() {
        return get().userId;
    }

    public static void remove() {
        TOKEN_AND_UID.remove();
    }
}
